/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.spider.comic.service.impl;

import me.zhengjie.spider.comic.domain.SpiderComicDetail;
import me.zhengjie.spider.comic.service.dto.SpiderComicDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.spider.comic.repository.SpiderComicDetailRepository;
import me.zhengjie.spider.comic.service.SpiderComicDetailService;
import me.zhengjie.spider.comic.service.dto.SpiderComicDetailDto;
import me.zhengjie.spider.comic.service.dto.SpiderComicDetailQueryCriteria;
import me.zhengjie.spider.comic.service.mapstruct.SpiderComicDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import me.zhengjie.utils.PageResult;

/**
 * @author 雨来
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-07-22
 **/
@Service
@RequiredArgsConstructor
public class SpiderComicDetailServiceImpl implements SpiderComicDetailService {

    private final SpiderComicDetailRepository spiderComicDetailRepository;
    private final SpiderComicDetailMapper spiderComicDetailMapper;

    @Override
    public PageResult<SpiderComicDetailDto> queryAll(SpiderComicDetailQueryCriteria criteria, Pageable pageable) {
        Page<SpiderComicDetail> page = spiderComicDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(spiderComicDetailMapper::toDto));
    }

    @Override
    public List<SpiderComicDetailDto> queryAll(SpiderComicDetailQueryCriteria criteria) {
        return spiderComicDetailMapper.toDto(spiderComicDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public SpiderComicDetailDto findById(Long comicDetailId) {
        SpiderComicDetail spiderComicDetail = spiderComicDetailRepository.findById(comicDetailId).orElseGet(SpiderComicDetail::new);
        ValidationUtil.isNull(spiderComicDetail.getComicDetailId(), "SpiderComicDetail", "comicDetailId", comicDetailId);
        return spiderComicDetailMapper.toDto(spiderComicDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SpiderComicDetail create(SpiderComicDetail resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setComicDetailId(snowflake.nextId());
        return spiderComicDetailRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SpiderComicDetail resources) {
        SpiderComicDetail spiderComicDetail = spiderComicDetailRepository.findById(resources.getComicDetailId()).orElseGet(SpiderComicDetail::new);
        ValidationUtil.isNull(spiderComicDetail.getComicDetailId(), "SpiderComicDetail", "id", resources.getComicDetailId());
        spiderComicDetail.copy(resources);
        spiderComicDetailRepository.save(spiderComicDetail);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long comicDetailId : ids) {
            spiderComicDetailRepository.deleteById(comicDetailId);
        }
    }

    @Override
    public void download(List<SpiderComicDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SpiderComicDetailDto spiderComicDetail : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("父id", spiderComicDetail.getComicId());
            map.put("标题", spiderComicDetail.getAlt());
            map.put("当前页面内容地址", spiderComicDetail.getHref());
            map.put("下一页", spiderComicDetail.getNextPage());
            map.put("是否删除0否 1是", spiderComicDetail.getDeletedFlag());
            map.put("更新时间", spiderComicDetail.getUpdateTime());
            map.put("创建时间", spiderComicDetail.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<SpiderComicDetailDto> findUnSpiderByWebId(Long webId) {
        List<SpiderComicDetail> unSpiderByWebId = spiderComicDetailRepository.findUnSpiderByWebId(webId);
        return spiderComicDetailMapper.toDto(unSpiderByWebId);
    }
}